from django.http import HttpResponse, HttpResponseRedirect
from django.shortcuts import render, redirect

# Create your views here.
from django.template.loader import render_to_string
from django.urls import reverse
from django.views.generic import TemplateView


def hello_world(request):
    print(reverse('hello_world'))
    return HttpResponse('hello world')

def hello_china(request):
    return HttpResponse('hello china')

def hello_html(request):
    #响应html内容
    html="""
    <html>
        <body>
            <h1 style="color:#f00">hello world</h1>
        </body>
    
    </html>
    """
    return HttpResponse(html)

def article_list(request,month):
    """
    :param month:今年某一个月的文章列表
    """
    return HttpResponse('article:'+month)

def search(request,name):
    if (int(name)%2)==0:
        return HttpResponse(name+'是偶数')


def render_str(request):
    """render_to_string函数的使用"""
    templ_name='index.html'
    html=render_to_string(template_name=templ_name)
    return HttpResponse(html)

def render_html(request):
    """render函数的使用"""
    return render(request,'index.html')

def http_request(request):
    #1.请求方式
    print(request.method)
    return HttpResponse('响应')
    #2.请求头信息
    headers=request.META
    print(headers)
    return HttpResponse('响应')
    #3.获取请求参数
    name=request.GET.get('name','')
    print(name)
    return HttpResponse('响应')

def no_data_404(request):
    return HttpResponse('404')

def article_detail(request,article_id):
    """
    文章详情：ID从1000开始的整数
    """
    if article_id<1000:
        #return HttpResponseRedirect('/hello/404/')
        return redirect('no_data_404')
    return HttpResponse('文章{}的内容'.format(article_id))

def index(request):
    return render(request,'index.html')

class HomeView(TemplateView):
    template_name='home.html'

# def index(request):
#     return render(request,'index.html')

def index(request):
    username='张三'
    age=48
    img_url='/media/1.jpg'
    list_users=[
        {'name':'zhangsan','age':34},
        {'name':'lisi','age':22},
    ]
    return render(request,'index.html',{
        'username':username,
        'age':age,
        'img_url':img_url,
        'list_users':list_users
    })

def tag(request):
    list_users = [
        {'name': 'zhangsan', 'age': 34},
        {'name': 'lisi', 'age': 22},
    ]
    return render(request,'tag.html',{
        'list_users': list_users
    })

def temple_test(request):
    list_users=[
        {'姓名':'小一','年龄':'25','性别':'女'},
    ]
    return render(request,'test.html',{
        'list_users':list_users
    })

def test_filter(request):
    """过滤器的使用"""
    name='model'
    return render(request,'test_filter.html',{
        'name':name
    })

def mine_filter(request):
    uname='张三的昵称'
    return render(request,'mine_filter.html',{
        'uname':uname
    })

def rearch(request):
    """GET参数的获取"""
    name=request.GET.get('name','')
    print(name)
    return HttpResponse('查询成功')